﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace DemoAstarAlgorithm
{
    class cGate
    {
        public struct InfoGate
        {
            public Texture2D m_tGate;
            public Vector2 m_v2Pos;
            public Point m_pFrameSize;
            public Point m_pCurrentFrame;
            public Point m_pSheetSize;

            public InfoGate(Texture2D newTexture, Vector2 newPos, Point newFrameSize, Point newSheetSize)
            {
                this.m_tGate = newTexture;
                this.m_v2Pos = newPos;
                this.m_pFrameSize = newFrameSize;
                this.m_pCurrentFrame = new Point(0, 0);
                this.m_pSheetSize = newSheetSize;
            }
        };

        InfoGate m_GateBorn;
        InfoGate m_GateDev;
        public int m_iTimeSinceLastFrame;
        public int m_iMillisecondsPerFrame;

        public cGate(ContentManager content)
        {
            m_GateBorn = new InfoGate(content.Load<Texture2D>(@"images/gate-born"), new Vector2(17, 40), new Point(185, 80), new Point(1, 8));
            m_GateDev = new InfoGate(content.Load<Texture2D>(@"images/Gate-Develop"), new Vector2(13 * 40 + 20, 40 + 11 * 40), new Point(120, 120), new Point(5, 3));
            m_iTimeSinceLastFrame = 0;
            m_iMillisecondsPerFrame = 250;
        }

        public void Update(GameTime gameTime, bool pause)
        {
            if (!pause)
            {
                m_iTimeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                if (m_iTimeSinceLastFrame > m_iMillisecondsPerFrame)
                {
                    m_iTimeSinceLastFrame -= m_iMillisecondsPerFrame;
                    ++m_GateBorn.m_pCurrentFrame.Y;
                    ++m_GateDev.m_pCurrentFrame.Y;
                    if (m_GateBorn.m_pCurrentFrame.Y >= m_GateBorn.m_pSheetSize.Y)
                    {
                        m_GateBorn.m_pCurrentFrame.Y = 0;
                        ++m_GateBorn.m_pCurrentFrame.X;
                        if (m_GateBorn.m_pCurrentFrame.X >= m_GateBorn.m_pSheetSize.X)
                            m_GateBorn.m_pCurrentFrame.X = 0;
                    }
                    if (m_GateDev.m_pCurrentFrame.Y >= m_GateDev.m_pSheetSize.Y)
                    {
                        m_GateDev.m_pCurrentFrame.Y = 0;
                        ++m_GateDev.m_pCurrentFrame.X;
                        if (m_GateDev.m_pCurrentFrame.X >= m_GateDev.m_pSheetSize.X)
                            m_GateDev.m_pCurrentFrame.X = 0;
                    }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(m_GateBorn.m_tGate, new Rectangle((int)m_GateBorn.m_v2Pos.X, (int)m_GateBorn.m_v2Pos.Y, m_GateBorn.m_pFrameSize.X, m_GateBorn.m_pFrameSize.Y - 40),
                                new Rectangle(m_GateBorn.m_pCurrentFrame.X * m_GateBorn.m_pFrameSize.X,
                                m_GateBorn.m_pCurrentFrame.Y * m_GateBorn.m_pFrameSize.Y, m_GateBorn.m_pFrameSize.X, m_GateBorn.m_pFrameSize.Y),
                                Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.88f);
            spriteBatch.Draw(m_GateDev.m_tGate, new Rectangle((int)m_GateDev.m_v2Pos.X, (int)m_GateDev.m_v2Pos.Y, m_GateDev.m_pFrameSize.X - 40, m_GateDev.m_pFrameSize.Y - 40),
                                 new Rectangle(m_GateDev.m_pCurrentFrame.X * m_GateDev.m_pFrameSize.X, m_GateDev.m_pCurrentFrame.Y * m_GateDev.m_pFrameSize.Y, m_GateDev.m_pFrameSize.X - 40, m_GateDev.m_pFrameSize.Y - 40),
                                 Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.88f);
        }
    }
}
